<?php

namespace App\Http\Controllers\backend;

use App\Models\Book;
use App\Models\Section;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class SectionController extends BaseController {
	
	public function index($bookId) {
		$book = Book::find($bookId);
		$sections = $book->sections()->select(['id', 'name'])->orderBy('order')->paginate(300);
		return view('backend.book', compact('book', 'sections'));
		
	}
	
	public function show($bookId, $sectionId) {
		$book = Book::find($bookId);
		$section = Section::find($sectionId);
		return view('backend.section', compact('book', 'section'));
	}
	
	public function edit($bookId, $sectionId) {
		$book = Book::find($bookId);
		$section = Section::find($sectionId);
		return view('backend.section_form', compact('book', 'section'));
	}
	
	public function update($bookId, $sectionId) {
		$book = Book::find($bookId);
		$section = Section::find($sectionId);
		$input = Input::all();
		$input['book_id'] = $book->id;
		$validation = validator($input, Section::$rules, Section::$msgs);
		if($validation->passes()) {
			$section->update($input);
			return redirect('backend/books/'.$book->id.'/sections/'.$section->id);
		}
		return Redirect::back()
				->withInput()
				->withErrors($validation);
	}

}
